package b2.widget.warehouse
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.player.dataModel.Mail;
	import b2.util.*;
	import b2.widget.BasicButton;
	import b2.widget.sendMail.SendMailTab;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.tech.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	
	public class Warehouse extends TabController
	{
		private static var instance:Warehouse;
		
		public function Warehouse()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("WarehouseBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr6");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr3");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			super(TabControllerBackground);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "仓库";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 13;
			addChild(titleText);
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - 42;
			closeButton.y = 12;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			
			var tab:Tab = new Tab("坦克", 3, 2);
			
			var rrect:Sprite = new Sprite();
			rrect.graphics.lineStyle(2.5, 0x27AAE1);
			rrect.graphics.beginFill(0x003752);
			for (var i:int = 0; i < 2; i++)
				for (var j:int = 0; j < 3; j++)
					rrect.graphics.drawRoundRect(j * 105, i * 105, 100, 100, 52, 52);
			rrect.graphics.endFill();
			rrect.filters = [new DropShadowFilter(2, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			tab.addChild(rrect);
			
			tab.setPageSelectorPos(110, 220);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("装备", 6, 4);
			//加载空槽位
			rrect = new Sprite();
			rrect.graphics.lineStyle(2.5, 0x27AAE1);
			rrect.graphics.beginFill(0x003752);
			for (i = 0; i < 4; i++)
				for (j = 0; j < 6; j++)
					rrect.graphics.drawRoundRect(j * 55, i * 55, 50, 50, 26, 26);
			rrect.graphics.endFill();
			rrect.filters = [new DropShadowFilter(2, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			tab.addChild(rrect);

			tab.setPageSelectorPos(110, 222);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			
			tab = new Tab("物品", 6, 4);
			//加载空槽位
			rrect = new Sprite();
			rrect.graphics.lineStyle(2.5, 0x27AAE1);
			rrect.graphics.beginFill(0x003752);
			for (i = 0; i < 4; i++)
				for (j = 0; j < 6; j++)
					rrect.graphics.drawRoundRect(j * 55, i * 55, 50, 50, 26, 26);
			rrect.graphics.endFill();
			rrect.filters = [new DropShadowFilter(2, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			tab.addChild(rrect);

			tab.setPageSelectorPos(110, 222);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			this.setTabTitlePos(28, 50); 
			this.setTabPagePos(24, 105);
		}

		public static function getInstance():Warehouse
		{
			if (!instance)
				instance = new Warehouse();
			return instance;
		}
		
		public function refresh():void
		{
			for (var i:int = 0; i < this.tabs.length; i ++)
			{
				this.tabs[i].removeAll();
			}
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var objectIndexArr:Array = new Array();
			var objectConfig:CommonObjConfig;
			//加载物品
			for (var objectComIndex:String in me.objPackage.objectList)
			{
				var objectComIndexArr:Array = objectComIndex.split("_");
				var objectIndex:int = objectComIndexArr[0];
				var tankId:int = objectComIndexArr[3];
				if (tankId == 0 && objectIndex != CommonObjConfig.EXP && objectIndex != CommonObjConfig.HONOUR &&   
					objectIndex != CommonObjConfig.FIGHT_POINT)
				{
					objectIndexArr.push(objectComIndex);
				}
			}
		    objectIndexArr.sort(sortObjects);
			for (i = 0; i < objectIndexArr.length; i++)
			{
				var comIndex:String = objectIndexArr[i];
				var objIndexArr:Array = comIndex.split("_");
				var objectNum:int = me.objPackage.objectList[comIndex];
				if (objectNum > 0)
				{
					var objectItem:ObjectItem = new ObjectItem(objIndexArr[0], objIndexArr[1], objIndexArr[2], objIndexArr[3], objectNum, 0);
					objectConfig = Config.objectConfigs[objIndexArr[0]];
					tabs[objectConfig.type].addItem(objectItem, 50, 50);
				}
			}
			//加载邮件
			for (var mailComIndex:String in me.mailPackage.mailList)
			{
				var mail:Mail = me.mailPackage.mailList[mailComIndex];
				var mailItem:MailItem = new MailItem(mail, parseInt(mailComIndex));
				tabs[2].addItem(mailItem, 50, 50);
			}
			var tankItem:TankItem;
			tankItem = new TankItem(me.campTankId);
			tabs[0].addItem(tankItem, 100, 100);
			//加载坦克
			for (i = 0; i < me.tankPackage.tankList.length; i++)
			{
				if (me.tankPackage.tankList[i] != me.campTankId)
				{
					tankItem = new TankItem(me.tankPackage.tankList[i]);
					tabs[0].addItem(tankItem, 100, 100);
				}
			}
			
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
		private function sortObjects(a:String, b:String):int
		{
			var aArray:Array = a.split("_");
			var bArray:Array = b.split("_");
			var objectConfigA:CommonObjConfig = Config.objectConfigs[aArray[0]];
			var objectConfigB:CommonObjConfig = Config.objectConfigs[bArray[0]];
			if (aArray[0] < bArray[0])
			{
				return -1;
			}
			else if (aArray[0] > bArray[0])
			{
				return 1;
			}
			else
			{
				//需要等级升序	
				if (objectConfigA.requireLevel < objectConfigB.requireLevel)
				{
					return -1;
				}
				else if (objectConfigA.requireLevel  > objectConfigB.requireLevel)
				{
					return 1;
				}
				else
				{	
					//basicLevel升序
					if (aArray[1] < bArray[1])
					{
						return -1;
					}
					else if (aArray[1] > bArray[1])
					{
						return 1;
					}
					else
					{
						//强化等级升序
						if (aArray[2] < bArray[2])
						{
							return -1;
						}
						else if (aArray[2] > bArray[2])
						{
							return 1;
						}
						else 
						{
							return 0;
						}
					}
				}
			}
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
			TankDialog.getInstance().close();
			TechTower.getInstance().close();
			SendMailTab.getInstance().close();
		}
		
	}
}